﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;
using System.Configuration;
using System.Data.SqlClient;


namespace StudentWeb.Api.Controllers
{
    public class bookController : ApiController
    {
        BookBO _BookBO = new BookBO();

        #region 学生信息 单条记录
        /// <summary>
        /// 书籍信息，单条记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        //[ValidToken]
        public IHttpActionResult GetBookInfo(int bookID)
        {
            try
            {
                var result = _BookBO.GetBookInfo(bookID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        public IHttpActionResult GetHello()
        {
            return Json("hello word");
        }
        #region 学生信息 全部记录
        [HttpGet]
        public IHttpActionResult GetBookList()
        {
            try
            {
                var result = _BookBO.GetBookList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }

        }
        #endregion



        #region 学生信息 shanchu单条记录
        /// <summary>
        /// 书籍信息，单条记录
        /// </summary>
        /// <param name="BookID"></param>
        /// <returns></returns>
        [HttpGet]
        //[ValidToken]
        public IHttpActionResult DeleteBookInfo([FromBody] JObject obj)
        {
            try
            {
                var result = _BookBO.DeleteBookInfo(obj["BookID"].ToObject<int>());
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 添加一条记录
        /// <summary>
        /// 书籍信息 添加一条记录
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]

        public IHttpActionResult InserBookInfo( [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _BookBO.InserBookInfo(model);
                return result ? Json(new Result(ResultCode.Success,"添加成功")) : Json(new Result(ResultCode.Fail, "添加失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 修改单条记录
        [HttpPost]
        public IHttpActionResult UpdateBookInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {

                var result = _BookBO.UpdataBookInfo(model);
                return result ? Json(new Result(ResultCode.Success, "修改成功")) : Json(new Result(ResultCode.Fail, "修改失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 修改单条记录
        public IHttpActionResult SaveBookInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {

                var result = _BookBO.SaveBookInfo(model);
                return result ? Json(new Result(ResultCode.Success, "修改成功")) : Json(new Result(ResultCode.Fail, "修改失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion


        #region 学生信息 分页列表
        /// <summary>
        /// 学生信息 分页列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetBookList([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail, "参数错误"));
                var wc = _GetBookListWhereC(model.condidate);
                var result = _BookBO.GetBookList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetBookListWhereC(dynamic qc)
        {
            var wc = string.Empty;
            if (qc == null) return wc;
            string tmp = "";
            //tmp = ConvertHelper.DynamicToString(qc.tabkey);
            //if (!string.IsNullOrEmpty(tmp) && tmp != "all") { wc += $" and (StudentState={tmp})"; }
            tmp = ConvertHelper.DynamicToString(qc.BookName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookName like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.BookNo);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookNo like '%{tmp}%'"; }
            return wc;
        }
        #endregion
 
    }
}
